<?php
/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2012 LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

// {{{ requires
require_once CLASS_EX_REALDIR . 'page_extends/LC_Page_Ex.php';
require_once CLASS_REALDIR . 'pages/pay/CallerService.php';

/**
 * 会員登録のページクラス.
 *
 * @package Page
 * @author LOCKON CO.,LTD.
 * @version $Id: LC_Page_Regist.php 21950 2012-07-02 11:42:51Z pineray $
 */
class LC_Page_Pay_Cancel extends LC_Page_Ex {

    // {{{ properties

    // }}}
    // {{{ functions
	
    /**
     * Page を初期化する.
     *
     * @return void
     */
    function init() {
    	
        parent::init();
        $this->tpl_mainpage = 'pay/cancel.tpl';
    	
    }

    /**
     * Page のプロセス.
     *
     * @return void
     */
    function process() {
        parent::process();
        $this->action();
        $this->sendResponse();
    }

    /**
     * Page のAction.
     *
     * @return void
     */
    function action() {

        switch ($this->getMode()) {
            case 'callOrder':
                break;
            case 'logout':
                break;
            default:
                break;
        }
    }

    /**
     * デストラクタ.
     *
     * @return void
     */
    function destroy() {
        parent::destroy();
    }
	/**
	 * 
	 * Enter description here ...
	 */
	function doPaypalTransaction(){
			$token = $_REQUEST['token'];
			if(! isset($token)) {
		
				/* The servername and serverport tells PayPal where the buyer
				   should be directed back to after authorizing payment.
				   In this case, its the local webserver that is running this script
				   Using the servername and serverport, the return URL is the first
				   portion of the URL that buyers will return to after authorizing payment
				   */
				   $serverName = $_SERVER['SERVER_NAME'];
				   $serverPort = $_SERVER['SERVER_PORT'];
				   $url=dirname('http://'.$serverName.':'.$serverPort.$_SERVER['REQUEST_URI']);
		
		
				   $currencyCodeType=$_REQUEST['currencyCodeType'];
				   $paymentType=$_REQUEST['paymentType'];
		   
		
		           $GOODSNAME		= $_REQUEST['GOODSNAME'];
				   $AMOUNT      	= $_REQUEST['AMOUNT'];
				   $QUANTITY        = $_REQUEST['QUANTITY'];
				   $TOTAL			= $_REQUEST['TOTAL'];
		
		
				 /* The returnURL is the location where buyers return when a
					payment has been succesfully authorized.
					The cancelURL is the location buyers are sent to when they hit the
					cancel button during authorization of payment during the PayPal flow
					*/
		
				   $returnURL =urlencode($url.'/pay/index.php?currencyCodeType='.$currencyCodeType.'&paymentType='.$paymentType);
				   $cancelURL =urlencode("$url/pay/cancel.php?paymentType=$paymentType" );
		
				 /* Construct the parameter string that describes the PayPal payment
					the varialbes were set in the web form, and the resulting string
					is stored in $nvpstr
					*/
		           $itemamt = 0.00;
		           $itemamt = $L_QTY0*$L_AMT0+$L_AMT1*$L_QTY1;
		           $amt = 5.00+2.00+1.00+$itemamt;
		           $maxamt= $amt+25.00;
		           $nvpstr="";
				   
		           /*
		            * Setting up the Shipping address details
		            */
		           $shiptoAddress = "&SHIPTONAME=$personName&SHIPTOSTREET=$SHIPTOSTREET&SHIPTOCITY=$SHIPTOCITY&SHIPTOSTATE=$SHIPTOSTATE&SHIPTOCOUNTRYCODE=$SHIPTOCOUNTRYCODE&SHIPTOZIP=$SHIPTOZIP";
		           
		           $nvpstr="&ADDRESSOVERRIDE=1$shiptoAddress&L_NAME0=".$L_NAME0."&L_NAME1=".$L_NAME1."&L_AMT0=".$L_AMT0."&L_AMT1=".$L_AMT1."&L_QTY0=".$L_QTY0."&L_QTY1=".$L_QTY1."&MAXAMT=".(string)$maxamt."&AMT=".(string)$amt."&ITEMAMT=".(string)$itemamt."&CALLBACKTIMEOUT=4&L_SHIPPINGOPTIONAMOUNT1=8.00&L_SHIPPINGOPTIONlABEL1=UPS Next Day Air&L_SHIPPINGOPTIONNAME1=UPS Air&L_SHIPPINGOPTIONISDEFAULT1=true&L_SHIPPINGOPTIONAMOUNT0=3.00&L_SHIPPINGOPTIONLABEL0=UPS Ground 7 Days&L_SHIPPINGOPTIONNAME0=Ground&L_SHIPPINGOPTIONISDEFAULT0=false&INSURANCEAMT=1.00&INSURANCEOPTIONOFFERED=true&CALLBACK=https://www.ppcallback.com/callback.pl&SHIPPINGAMT=8.00&SHIPDISCAMT=-3.00&TAXAMT=2.00&L_NUMBER0=1000&L_DESC0=Size: 8.8-oz&L_NUMBER1=10001&L_DESC1=Size: Two 24-piece boxes&L_ITEMWEIGHTVALUE1=0.5&L_ITEMWEIGHTUNIT1=lbs&ReturnUrl=".$returnURL."&CANCELURL=".$cancelURL ."&CURRENCYCODE=".$currencyCodeType."&PAYMENTACTION=".$paymentType;
				   
		           $nvpstr = $nvpHeader.$nvpstr;
		           
				 	/* Make the call to PayPal to set the Express Checkout token
					If the API call succeded, then redirect the buyer to PayPal
					to begin to authorize payment.  If an error occured, show the
					resulting errors
					*/
				   $resArray=hash_call("SetExpressCheckout",$nvpstr);
				   $_SESSION['reshash']=$resArray;
		
				   $ack = strtoupper($resArray["ACK"]);
		
				   if($ack=="SUCCESS"){
							// Redirect to paypal.com here
							$token = urldecode($resArray["TOKEN"]);
							$payPalURL = PAYPAL_URL.$token;
							header("Location: ".$payPalURL);
						  } else  {
							 //Redirecting to APIError.php to display errors.
								$location = "APIError.php";
								header("Location: $location");
							}
		} else {
				 /* At this point, the buyer has completed in authorizing payment
					at PayPal.  The script will now call PayPal with the details
					of the authorization, incuding any shipping information of the
					buyer.  Remember, the authorization is not a completed transaction
					at this state - the buyer still needs an additional step to finalize
					the transaction
					*/
		
				   $token =urlencode( $_REQUEST['token']);
		
				 /* Build a second API request to PayPal, using the token as the
					ID to get the details on the payment authorization
					*/
				   $nvpstr="&TOKEN=".$token;
		
				   $nvpstr = $nvpHeader.$nvpstr;
				 /* Make the API call and store the results in an array.  If the
					call was a success, show the authorization details, and provide
					an action to complete the payment.  If failed, show the error
					*/
				   $resArray=hash_call("GetExpressCheckoutDetails",$nvpstr);
				   $_SESSION['reshash']=$resArray;
				   $ack = strtoupper($resArray["ACK"]);
		
				   if($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING'){
							require_once "GetExpressCheckoutDetails.php";
					  } else  {
						//Redirecting to APIError.php to display errors.
						$location = "APIError.php";
						header("Location: $location");
					  }
		}
	}




}
